/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package processing.logic;

import processing.objects.Result;
import processing.objects.SearchParameters;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class handles matching messages according to given regexp/string
 * Search criteria
 *
 * @author rymek
 *         User: rymek
 *         Date: 15.07.11
 *         Time: 12:48
 */
@SuppressWarnings({"ConstantConditions"})
public class MessageParser {

    private final String regExpString;          // prepared regexp search string

    /**
     * Constructor.
     * Calls method to parse input string to add escape characters
     * for RegExp matching
     *
     * @param searchParameters search parameters
     */
    public MessageParser(SearchParameters searchParameters) {
        this.regExpString = searchParameters.getSearchString();
    }

    /**
     * Performs message body parsing
     *
     * @param result the result to parse
     * @return true if result matches regexp criteria
     */
    public boolean parseMessageBody(Result result) {

        Pattern pattern = Pattern.compile(this.regExpString, Pattern.COMMENTS);
        Matcher matcher = pattern.matcher(result.getXmlString());

        return matcher.find();
    }

    /**
     * Adds escape characters to special characters
     *
     * @param input input string to transform
     * @return parsed Regular Expression String
     */
    public static String prepareRegexpString(String input) {

        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator(input);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            switch (character) {
                case '<':
                    result.append("&lt;");
                    break;
                case '>':
                    result.append("&gt;");
                    break;
                case '\"':
                    result.append("&quot;");
                    break;
                case '\'':
                    result.append("&#039;");
                    break;
                case '&':
                    result.append("&amp;");
                    break;
                default:
                    //the char is not a special one
                    //add it to the result as is
                    result.append(character);
                    break;
            }
            character = iterator.next();
        }
        return result.toString();
    }

    @Override
    public String toString() {
        return "MessageParser{" +
                "regExpString='" + regExpString + '\'' +
                '}';
    }
}
